package com.wxjfh.servlet;

import com.wxjfh.Utils.DataUtils;
import com.wxjfh.bean.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet(urlPatterns = "/register")
public class RegisterServlet  extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username= req.getParameter("username");
        String password= req.getParameter("password");
        String againpassword= req.getParameter("againpassword");
        String name= req.getParameter("name");
        String sex= req.getParameter("sex");
        String birthday= req.getParameter("birthday");
        String[] powers= req.getParameterValues("power");
        String jiguan= req.getParameter("jiguan");
        System.out.println(username+ " "+password+" "+againpassword+" "+name+" "+sex+" "+birthday+" "+ Arrays.toString(powers) +" "+jiguan+"");
        if( password !=null ||password.equals(" ")|| !password.equals(againpassword)){
            resp.sendRedirect("register.jsp?status=1");
            return;
        }
        if(username==null|| username.equals("")){
            resp.sendRedirect("register.jsp?status=2");
            return;
        }
        Admin admin=new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
        admin.setSex(sex);
        admin.setBirthday(birthday);
        admin.setPowers(powers);
        admin.setJiguan(jiguan);


        DataUtils.addAdmin(admin);
        resp.sendRedirect("login.jsp");

    }
}
